package cn.janescott.file.server.exception;

/**
 * 自定义异常
 *
 * @author scott
 * @date 2019/4/15
 */
public class MyException extends RuntimeException {
    /**
     * 异常代码
     */
    private String code;

    /**
     * 构造方法
     */
    private MyException(String message, Throwable cause) {
        super(message, cause);
    }

    /**
     * 未知异常
     *
     * @param message
     * @param th
     * @return
     */
    public static MyException failed(String message, Throwable th) {
        MyException me = new MyException(message, th);
        me.setCode(MyExceptionEnums.UNKNOWN_EXCEPTION.getCode());
        return me;
    }

    /**
     * 系统异常
     *
     * @param code
     * @param message
     * @return
     */
    public static MyException failed(String code, String message) {
        MyException me = new MyException(message, null);
        me.setCode(code);
        return me;
    }

    /**
     * 运行时异常
     *
     * @param enums
     * @param th
     * @return
     */
    public static MyException failed(MyExceptionEnums enums, Throwable th) {
        MyException me = new MyException(enums.getMessage(), th);
        me.setCode(enums.getCode());
        return me;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return super.getMessage();
    }

    @Override
    public synchronized Throwable getCause() {
        return super.getCause();
    }
}
